package com.ubs.training.server.dao.impl.hbm;

import com.ubs.training.server.dao.EmployeeDao;
import com.ubs.training.server.dao.model.Employee;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/27/12
 * Time: 3:22 AM
 * To change this template use File | Settings | File Templates.
 */
@Repository("hbmEmployeeDao")
public class HbmEmployeeDao implements EmployeeDao {

    private SessionFactory factory;

    private HibernateTemplate tpl;

    public SessionFactory getFactory() {
        return factory;
    }

    @Resource(name="sessionFactory")
    public void setFactory(SessionFactory factory) {
        tpl = new HibernateTemplate(factory);
        this.factory = factory;
    }

    @Override
    public List<Employee> getAll() {
        return tpl.loadAll(Employee.class);
    }

    @Override
    public Employee getById(int id) {
        return tpl.load(Employee.class,id);
    }

    @Override
    public void add(Employee e) {

        tpl.save(e);
    }

    @Override
    public void update(Employee e) {
        tpl.merge(e);
    }

    @Override
    public void delete(int id) {
        Employee empl = (Employee) factory.getCurrentSession().get(Employee.class, id);
        tpl.delete(empl);
    }
}
